package games.ninJump.ui.effects
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import games.ninJump.data.GameState;
	
	import interfaces.IUpdate;
	
	import utils.Particle;
	
	public class WallMove extends Sprite implements IUpdate
	{
		private var m_gameState:GameState;
		private var m_walls:Vector.<Particle>;
		private var m_bmd:BitmapData;
		private var m_multiple:Number = .25;
		private var m_speed:Number;
		
		public function WallMove(gs:GameState)
		{
			m_walls = new Vector.<Particle>;
			m_gameState = gs;
			speed = m_gameState.speed * m_multiple;
			m_gameState.addEventListener(Event.CHANGE,onChangeState);
		}
		
		public function update(time:Object=null):void
		{
			var t:Number = Number(time);
			var wall:Sprite = createWallTile();
			var particle:Particle = new Particle(wall,this);
			m_walls.push(particle);
			while(m_walls.length > 10)
			{
				particle = Particle(m_walls.shift());
				particle.destroy();
			}
			
			for(var i:Number = 0;i< m_walls.length;i++)
			{
				// and update each one
				m_walls[i].setPos(0,(m_walls[i].clip.height * i + t * m_speed ) % (GameState.GAME_HEIGHT + m_walls[i].clip.height) -  m_walls[i].clip.height);
				//m_particles[i].update();
			}
		}
		
		public function set bmd(n:BitmapData):void
		{
			m_bmd = n;
		}
		
		public function set speed(n:Number):void
		{
			m_speed = n;
		}
		
		private function onChangeState(e:Event):void
		{
			speed = GameState(e.target).speed * m_multiple ;
		}
		
		private function createWallTile():Sprite
		{
			var sp:Sprite = new Sprite();
			var bm:Bitmap;
			
			for(var i:int = 0; i < 2; i++)
			{
				bm = new Bitmap(m_bmd);
				bm.smoothing = true;
				bm.scaleX = (i%2 == 0? 1 : -1);
				bm.x = (i%2 == 0?0:GameState.GAME_WIDTH);
				//bm.y = m_game.bg.height - bm.height / 2 - int (i / 2) * bm.height ;
				
				sp.addChild(bm);
			}
			return sp;
		}
	}
}